﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;
using Telecom.Service.EntityCustom;

namespace Telecom.Service
{
    public class _LapTBService
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public _LapTBService()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public decimal addorUpdate(LAP_THIETBI entity)
        {
            if (entity.ID == 0) dbContext.LAP_THIETBI.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.ID;
        }

        public ListObjectToJtable<LAP_THIETBI> getList(string[] toquanly, string[] donviQl, int[] StiedId, string mangvt, int jtStartIndex = 0, int jtPageSize = 0)
        {
            ListObjectToJtable<LAP_THIETBI> _listReturnOjnect = new ListObjectToJtable<LAP_THIETBI>();

            try
            {
                //var _listReturnALl = dbContext.LAP_THIETBI.ToList();

                var _listReturnALl = (from i in dbContext.LAP_THIETBI
                                      join l in dbContext.DM_THIETBI on i.DMTHIETBI equals l.ID
                                      join k in dbContext.LOAI_TBI on l.LOAITB equals k.MA
                                      where k.MANGVT == mangvt 
                                      select i).ToList();

                if (toquanly != null)
                {
                    if (_listReturnALl != null)
                    {
                        _listReturnALl = _listReturnALl.Where(p => (toquanly.Contains(p.TOQL))).ToList();
                    }
                }

                if (donviQl != null)
                {
                    if (_listReturnALl != null)
                    {
                        _listReturnALl = _listReturnALl.Where(p => (donviQl.Contains(p.DONVIQL))).ToList();
                    }
                }

                if (StiedId != null)
                {
                    if (_listReturnALl != null)
                    {

                        _listReturnALl = _listReturnALl.Where(p => (StiedId.Contains(p.SITEID.Value))).ToList();
                    }
                }
                
               _listReturnOjnect.count = _listReturnALl.Count();

                if (jtPageSize != 0)
                {
                    _listReturnALl = _listReturnALl.Skip(jtStartIndex).Take(jtPageSize).ToList();
                }

                _listReturnOjnect.list = _listReturnALl;

                return _listReturnOjnect;
                
            }
            catch (Exception e)
            {
                ListObjectToJtable<LAP_THIETBI> _listRe = new ListObjectToJtable<LAP_THIETBI>();
                return _listRe;
            }
        }

        public List<LAP_THIETBI> GetListByToQLMangvtDN(string toql, string mangVT, int id)
        {
            try
            {
                var _listReturnALl = (from i in dbContext.LAP_THIETBI
                                      join l in dbContext.DM_THIETBI on i.DMTHIETBI equals l.ID
                                      join k in dbContext.LOAI_TBI on l.LOAITB equals k.MA
                                      where k.MANGVT == mangVT && i.TOQL == toql && i.ID != id
                                      orderby i.ID descending
                                      select i);

                return _listReturnALl.ToList();
                
            }
            catch (Exception e)
            {
                List<LAP_THIETBI> _listRe = new List<LAP_THIETBI>();
                return _listRe;
            }
        }

        public LAP_THIETBI GetById(int Id)
        {
            try
            {
                return dbContext.LAP_THIETBI.Where(p => p.ID == Id).First();
            }
            catch (Exception ex)
            {
                LAP_THIETBI _ml = new LAP_THIETBI();
                return _ml;
            }
        }

        public LAP_THIETBI GetByIdMa(string Id)
        {
            try
            {
                return dbContext.LAP_THIETBI.Where(p => p.MA == Id).First();
            }
            catch (Exception ex)
            {
                LAP_THIETBI _ml = new LAP_THIETBI();
                return _ml;
            }
        }

        public bool Detele(int Id)
        {
            try
            {
                var _entity = GetById(Id);
                dbContext.LAP_THIETBI.Remove(_entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex) { return false; }
        }

        public bool CheckLapTbDaDauNoi(int Id)
        {
            try
            {
                var _entity = dbContext.PORT.Where(prop => prop.TB_OUT == Id).First();
                if (_entity != null)
                    return true;
                else return false;
            }
            catch (Exception ex) { return false; }
        }


        #region Đấu nối thiêt bị

        /// <summary>
        /// Đấu nối thiết bị
        /// Tìm thiết bị đấu nối vào
        /// </summary>
        /// <param name="lapTbId"></param>
        /// <returns></returns>

        public int FindTBInOfTb_On_LapTB(int lapTbId)
        {
            try
            {
                var CauHinhTb = dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == lapTbId).First();
                var ListPortByCauHinh = dbContext.PORT.Where(p => p.CAUHINHID == CauHinhTb.ID).First();
                return ListPortByCauHinh.TB_IN.Value;
            }
            catch (Exception ex) { return 0; }
        }

        public int FindPortDauNoiForLapTB(int lapTbId)
        {
            try
            {
                return dbContext.PORT.Where(p => p.TB_OUT == lapTbId).First().ID;
            }
            catch (Exception ex) { return 0; }
        }

        public int FindCardDauNoiForLapTB(int lapTbId)
        {
            try
            {
                var PortEntity = dbContext.PORT.Where(p => p.TB_OUT == lapTbId).First();
                return PortEntity.CAUHINHID.Value;
            }
            catch (Exception ex) { return 0; }
        }


        #endregion


    }
}
